package com.burger.counter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.burger.counter.Constants.SQLConstants;
import com.burger.counter.beans.Order;
import com.burger.counter.util.DBConnection;

public class OrderDao {
	
	public Order[] getMaxOrder(){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Order maxOrder[] = null;
		int size;
		
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.maxOrderIdFromOrder;
			prest = con.prepareStatement(prepStatement);
			
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				maxOrder = new Order[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Order p = new Order();
					p.setOrderId(rs.getInt(1));
					maxOrder[currRow] = p;
					currRow++;
				}
			}			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return maxOrder;
	}

	public void insertOrderDetails(int orderId, int userId, int regionId, String orderDate, double price, int quantity, int itemId, String orderPickUpDate){
        Connection con=null;
        PreparedStatement prest =null;
 
        try{
            con=DBConnection.getConnection();
            System.out.println("Creating DB Connection");
            String prepStatement=SQLConstants.insertIntoOrder;
            prest = con.prepareStatement(prepStatement);
            
            prest.setInt(1, orderId);
            prest.setInt(2, userId);
            prest.setInt(3, regionId);
            prest.setString(4, orderDate);
            prest.setDouble(5, price);
            prest.setInt(6, quantity);
            prest.setInt(7, itemId);
            prest.setString(8, orderPickUpDate);
            
            prest.executeUpdate();
            
            System.out.println("An order got added");
        }
        catch(Exception e){
            
        }
	}  
	
	public Order[] getOrderDetails(int userId){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		Order[] orderList=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.getOrderDetails;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1, userId);
			
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				orderList = new Order[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					Order p = new Order();
					p.setOrderId(rs.getInt(1));
					p.setUserId(rs.getInt(2));
					p.setRegionId(rs.getInt(3));
					p.setOrderDate(rs.getDate(4));
					p.setPrice(rs.getDouble(5));
					p.setQuantity(rs.getInt(6));
					p.setItemId(rs.getInt(7));
					p.setPickupDate(rs.getDate(8));
					orderList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			
		}
		
		return orderList;
	}
}
